//
//  YQNetWorkRequestManager.m
//  EPA--1
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "YQNetWorkRequestManager.h"

@interface YQNetWorkRequestManager ()
/**
 *  存储回调Block的属性
 */
//@property (nonatomic, copy) ResultBlock block;

@end
@implementation YQNetWorkRequestManager
/**
 *  单例
 *
 *  @return
 */
+ (instancetype)defaultManager
{
    {
        static YQNetWorkRequestManager *manager = nil;
        @synchronized(self) {
            if (!manager) {
                manager = [[YQNetWorkRequestManager alloc]init];
            }
        }
        return manager;
    }

}
/**
 *  初始化方法
 *
 *  @param url         链接
 *  @param method      请求方式
 *  @param param       请求参数
 *  @param resultBlock 储存返回数据的block
 */
+ (void)requestURL:(NSURL *)url method:(URLRequestMethod)method params:(NSDictionary *)param result:(ResultBlock)resultBlock
{
    //空的可变字符串
    NSMutableString *string = [[NSMutableString alloc]init];
    if ([url isKindOfClass:[NSURL class]]) {
        //拼接Url
        [string appendString:url.absoluteString];
    
    }else if ([url isKindOfClass:[NSString class]]) {
        [string appendString:(NSString *)url];
    }
    if (string.length == 0) {
        NSError *error = [NSError errorWithDomain:@"EPA" code:251 userInfo:@{@"reason":@"你输入的网址不正确"}];
        resultBlock(nil,nil,error);
        return;
    }
    //吧参数字典转化为字符串
    YQNetWorkRequestManager *manager = [YQNetWorkRequestManager new];
//    manager.block = resultBlock;
    NSString *paraStr = [manager strFromDict:param];
    //两种请求方式
    switch (method) {
        case YQ_GET:
        {
            if (paraStr.length > 0) {
                [string appendFormat:@"?%@",paraStr];
            }
            //这句话应该没有吧???
//            [string appendString:paraStr];
            [manager requestURL:[NSURL URLWithString:string] resultBlock:resultBlock];
          
        }
            break;
        case YQ_POST:
            [manager postURL:string paraStr:paraStr resultBlock:resultBlock];
        default:
            break;
    }
}
/**
 *  get的网络请求
 
 */
- (void)requestURL:(NSURL *)url resultBlock:(ResultBlock)block
{
//    __weak typeof (self) weakSelf = self;
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithURL:url completionHandler:block];
    //开始网络请求
    [dataTask resume];
}

/**
 *  post的网络请求
 *
 *  @param urlStr  网址
 *  @param paraStr 请求体(?后边的东西)
 */
- (void)postURL:(NSString *)urlStr paraStr:(NSString *)paraStr resultBlock:(ResultBlock)block
{
    NSURL *url = [NSURL URLWithString:urlStr];
    //创建可变的请求
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    //设置请求体
    [mRequest setHTTPBody:[paraStr dataUsingEncoding:NSUTF8StringEncoding]];
    //设置POST方法
    [mRequest setHTTPMethod:@"post"];
//    __weak typeof (self) weakSelf = self;
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:mRequest completionHandler:block];
    [dataTask resume];
}
- (NSString *)strFromDict:(NSDictionary *)dict
{
//    //空字符串
//    NSMutableString *string = [NSMutableString stringWithFormat:@"?"];
    //空数组
    NSMutableArray *array = [NSMutableArray new];
    for (NSString *key in dict) {
        NSString *str = [NSString stringWithFormat:@"%@=%@",key,dict[key]];
        [array addObject:str];
    }
    //数组拼接字符串
    NSString *para = [array componentsJoinedByString:@"&"];
//    NSString *pa = [array componentsJoinedByString:@"$"];
     NSLog(@"%@",para);
//     [string appendString:para];
//    return string;
    return para;
}
    

@end
